package com.itcast.plane.modoule.planes.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.common.ConsHint;
import com.itcast.plane.common.PageIndex;
import com.itcast.plane.modoule.flight.domain.Flight;
import com.itcast.plane.modoule.flight.mapper.FlightMapper;
import com.itcast.plane.modoule.planes.domain.Planes;
import com.itcast.plane.modoule.planes.dto.PlanesCreatDTO;
import com.itcast.plane.modoule.planes.mapper.PlanesMapper;
import com.itcast.plane.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PlanesService extends ServiceImpl<PlanesMapper, Planes> {

    @Resource
    private PlanesMapper planesMapper;

    @Resource
    private FlightMapper flightMapper;

    public JsonResult addPlanes(PlanesCreatDTO creatDTO) {
        Planes plane = new Planes();
        BeanUtils.copyProperties(creatDTO, plane);
        int count = count(plane);
        if (count == 0) {
            planesMapper.insert(plane);
            return new JsonResult(true, ConsHint.INSERT_SUCCESS);
        } else {
            return new JsonResult(false, ConsHint.INSERT_DEFAULTED);
        }
    }

    public JsonResult deletePlanes(int id) {
        LambdaQueryWrapper<Flight> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int i = flightMapper.selectCount(lambdaQueryWrapper.eq(Flight::getPathId, id));
        if (i>0) {
            return new JsonResult(false, ConsHint.DELETE_DEFAULT_USING);
        }
        planesMapper.deleteById(id);
        return new JsonResult(true, ConsHint.DELETE_SUCCESS);
    }

    public JsonResult editPlanes(Planes planes) {
        int count = count(planes);
        if (count == 0) {
            planesMapper.updateById(planes);
            return new JsonResult(true, ConsHint.EDIT_SUCCESS);
        } else {
            return new JsonResult(false, ConsHint.EDIT_DEFAULT_USING);
        }
    }

    public JsonResult getPage(int pages) {
        LambdaQueryWrapper<Planes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Planes::getIfDelete,0).orderByDesc(Planes::getId);
        IPage<Planes> page = page(new Page<>(pages, PageIndex.pageSize), lambdaQueryWrapper);
        return new JsonResult(true,  page);
    }

    public JsonResult getAll() {
        LambdaQueryWrapper<Planes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Planes> list = planesMapper.selectList(lambdaQueryWrapper.eq(Planes::getIfDelete, 0));
        return new JsonResult(true, list);
    }

    private int count(Planes planes) {
        LambdaQueryWrapper<Planes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Planes> list = planesMapper.selectList(lambdaQueryWrapper.eq(Planes::getName, planes.getName()));
        int count = 0;
        for (Planes planes1 : list) {
            if (planes1.getName().equals(planes.getName())) {
                count++;
            }
        }
        return count;
    }

}
